<template>
	<view class="configuration">
		<view class="top-box">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>链接管理</el-breadcrumb-item>
				<el-breadcrumb-item>{{ getBreadcrumb }}</el-breadcrumb-item>
			</el-breadcrumb>
			
			<view class="header-serch flex ac jcsb">
				<view class="search-box flex ac">
					<view class="search flex ac" v-if="second">
						<view class="search-title min-w60">链接ID：</view>
						<el-input v-model="name" placeholder="请输入链接ID" style="height: 32px;"></el-input>
					</view>
					<!-- <view class="search flex ac">
						<view class="search-title min-w108" :class="{ 'min-w116': second }">{{ second? '广告位':'媒体' }}状态：</view>
						<el-select v-model="value" placeholder="请选择">
							<el-option
							  v-for="t in selectArr"
							  :key="t.value"
							  :label="t.label"
							  :value="t.value">
							</el-option>
						</el-select>
					</view> -->
					<view class="search-btn" v-if="second">
						<el-button type="primary" class="search-button" @click='search'>查询</el-button>
					</view>
				</view>

				<el-button class="add-user" type="primary" @click='openDelog(0)' v-if="!second">+ 新增广告主</el-button>
			</view>
			
			<el-table 
				:data="Table.TableDataList" 
				style="width: 100%;" 
				height="calc(100vh - 298px)"
				stripe
				:header-cell-style="{background:'#E9ECEE',color:'#16171F'}">
				<el-table-column 
					show-overflow-tooltip 
					:prop="item.prop" 
					:label="item.label" 
					v-for="(item,index) in newTitleList"
					:key="index"
					:min-width="item.width">
					<template slot-scope="scope">
						<!-- 每一项的值 -->
						<span v-if="item.prop && item.prop != 'linkUrl' && item.prop != 'url'">{{ item.prop == 'createTime' ? new Date(scope.row[item.prop]).format():scope.row[item.prop] }}</span>
						
						<!-- 广告主链接  千集跳转链接 -->
						<span v-if="item.prop && (item.prop == 'linkUrl' || item.prop == 'url')" @click="copyClick(scope.row, item.prop)">{{ scope.row[item.prop] }}</span>
						
						<!-- 操作项 -->
						<!-- 广告主 -->
						<view v-if="!item.prop && !second">
							<el-button class="del-color" @click="delClick(scope.row, 0)" type="text" size="small">删除</el-button>
						</view>
					</template>
				</el-table-column>
			</el-table>
		</view>
		<view class="footer">
			<el-pagination  
				@size-change="handleSizeChange" 
				@current-change="handleCurrentChange"
				:current-page="Table.page"  
				layout="total, prev, pager, next, jumper, sizes" 
				background 
				:total="Table.count"
				:page-sizes="[10, 12]" 
				:page-size.sync="Table.limit">
			</el-pagination>
			<view class="pageLimit">当前显示{{Table.limit}}条/页</view>
		</view>
		
		<confirm @confirmFlagClick="confirmFlagClick" :confirmFlag="confirmFlag" :title="confirmTitle"></confirm>
		<linkManagementDialog :dialogVisible="addUserDialogFlag" @closeDialog="closeDialog" :item="currentUpdate" :title="addDialogTitle"></linkManagementDialog>
	</view>
</template>

<script>
	import { Request } from '../model/request.js'
	import { Util } from '../utils/util.js'
	import confirm from './dialog/confirm.vue'
	import linkManagementDialog from './dialog/linkManagementDialog.vue'
	 
	export default {
		components: { confirm, linkManagementDialog },
		
		props: {
			second: {
				type: [String, Number],
				default: 0
			}
		},
		
		computed: {
			getBreadcrumb() {
				let c = '广告主链接'
				if (this.second == 1) {
					c = '曝光数据'
				}
				return c
			},
			
			newTitleList() {
				if (this.second == 1) {
					return this.Table.titleList1
				}else {
					return this.Table.titleList
				}
			}
		},
		
		data() {
			return {
				phone: '',
				name: '',
				selectArr: [
					{
					  value: '',
					  label: '全部'
					}, {
					  value: '0',
					  label: '可用'
					}, {
					  value: '1',
					  label: '不可用'
					}
				],
				value: '',
				Table: {
					titleList: [{
							prop: "advertingName",
							label: "广告主"
						},
						{
							prop: "linkUrl",
							label: "广告主链接",
							width: '130px'
						},
						{
							prop: "url",
							label: "千集跳转地址",
							width: '130px'
						},
						{
							prop: "createTime",
							label: "创建时间",
							width: '60px'
						},
						// {
						// 	prop: "secretkey",
						// 	label: "appSecret"
						// },
						// {
						// 	prop: "appkey",
						// 	label: "appKey"
						// },
						{
							// prop: "mediaStatus",
							label: "操作",
							width: '60px'
						},
					],
					titleList1: [{
							prop: "activityId",
							label: "链接id",
							width: '40px'
						},
						{
							prop: "jumpUrl",
							label: "广告主链接",
							width: '130px'
						},
						{
							prop: "qjUrl",
							label: "千集跳转地址",
							width: '130px'
						},
						{
							prop: "exposureNumber",
							label: "曝光次数",
							width: '40px'
						},
						// {
						// 	label: "操作"
						// }
					],
					TableDataList: [],
					result: '',
					page: 1,
					limit: 10,
					count: 0
				},
				
				confirmFlag: false,
				confirmTitle: '',
				confirmType: '',
				currentDelItem: {},
				
				addUserDialogFlag: false,
				addDialogTitle: '',
				currentUpdate: {}
			}
		},
		
		mounted() {
			this.initTable()
		},
		
		watch: {
			second(newVal, oldVal) {
				this.phone = ''
				this.name = ''
				this.value = ''
				this.Table.page = 1
				this.Table.limit = 10
				this.Table.count = 0
				this.initTable()
			}
		},
		
		methods: {
			getMediaStatus(i) {
				if (i) {
					return '不可用'
				} else {
					return '可用'
				}
			},
			
			search() {
				this.initTable()
			},
			
			async initTable(page) {
				this.Table.TableDataList = []
				if (!Util.isNull(page)) {
					this.Table.page = page
				} else {
					this.Table.page = 1
				}
				let res
				if (this.second == 1) { // 曝光数据
					res = await Request.SALESFINDJUMPLOG(this.Table.page, this.Table.limit, this.name)
				} else { 
					res = await Request.SALESFIND(this.Table.page, this.Table.limit, this.name, this.value)
				}
				console.log(res, 'resswss')
				if (!Util.isNull(res)) {
					this.Table.TableDataList = res.data.records
					this.Table.count = res.data.total
				} else {
					this.Table.TableDataList = null
				}
			},
			
			openDelog(i) {
				// if (i) {
				// 	this.addDialogTitle = '新增链接'
				// } else {
				// 	this.addDialogTitle = '创建用户'
				// }
				this.addDialogTitle = '新增广告主'
				this.addUserDialogFlag = true
			},
			
			closeDialog(flag) {
				this.currentUpdate = {}
				this.addUserDialogFlag = false
				if (typeof flag != 'function' && flag) {
					this.initTable()
				}
			},
			
			delClick(item, i) {
				if (i) { //
					// this.confirmTitle = '确认删除该项活动配置？'
				} else { // 广告主
					this.confirmTitle = `确认删除广告主：${item.advertingName}？`
				}
				this.confirmFlag = true
				this.currentDelItem = item
			},
			
			// 确认框触发事件
			async confirmFlagClick(i) {
				if (i) { // 确认
					let res = await Request.SALESDELETE(this.currentDelItem.id)
					if (res.code == 200) {
						Util.showToast('删除成功')
						this.initTable()
					}
				}
				this.confirmFlag = false
			},
			
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.initTable()
			},
			
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.initTable(val)
			},
			
			copyClick(item, type) { // 复制
				Util.copy(item[type])
			},
		}
	}
</script>

<style scoped>
	.configuration {
		/* padding: 20px 40px 50px; */
		position: relative;
	}
	
	.top-box {
		padding: 20px 40px 0;
	}
	
	.header-serch {
		margin: 30px 0;
	}
	
	.search-title {
		/* width: 100px; */
		font-size: 14px;
		font-weight: 500;
		color: #16171F;
		text-align: right;
	}
	
	.search-btn {
		margin-left: 30px;
	}
	
	.footer {
		width: 100%;
		height: 40px;
		display: flex;
		align-items: center;
		/* position: absolute; */
		/* left: 0; */
		/* bottom: 0px; */
		justify-content: center;
		height: 130px;
		background: #FFFFFF;
		box-shadow: 0px -7px 9px 0px rgba(0, 0, 0, 0.02);
	}
	
	.del-color {
		color: #D1141F;
	}
	
	.start-color {
		color: #4A92FD;
	}
	
	.yes {
		color: #69BC2E;
	}
	
	.no {
		color: #D1141F;
	}
	
	>>> .el-input__inner{
		height: 32px;
	}
	
	.search-button, .add-user {
		font-size: 14px;
		height: 32px;
		line-height: 32px;
		padding-top: 0;
		padding-bottom: 0;
	}
	
	/* 选择框 行高 */
	>>> .el-input__icon {
		line-height: 28px;
	}
	
	.min-w58 {
		min-width: 58px;
	}
	
	.min-w72 {
		min-width: 72px;
	}
	
	.min-w86 {
		min-width: 86px;
	}
	
	.min-w100 {
		min-width: 100px;
	}
	
	.min-w108 {
		min-width: 108px;
	}
	
	.min-w116 {
		min-width: 116px;
	}
	
	.w74 {
		width: 74px;
	}
	
	.min-w60 {
		min-width: 60px;
	}
</style>
